package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook, HttpServletRequest request){
        //通过session中存储的UserId
        log.info("地址为：{}",addressBook);
        Long userId = (Long) request.getSession().getAttribute("user");
        addressBook.setUserId(userId);

        log.info("地址为：{}",addressBook);
        addressBookService.save(addressBook);
        return R.success("添加地址成功");
    }

    @Transactional
    @PutMapping("/default")
    public R<AddressBook> updateDefault(@RequestBody AddressBook addressBook, HttpSession session){

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        log.info("ID;{}",BaseContext.getCurrentId());
        log.info("ID;{}",session.getAttribute("user"));

        /*queryWrapper.eq(AddressBook::getIsDefault,1).
                    eq(AddressBook::getUserId,BaseContext.getCurrentId());*/

        queryWrapper.eq(AddressBook::getIsDefault,1).
                eq(AddressBook::getUserId,session.getAttribute("user"));

        AddressBook addressDefault = addressBookService.getOne(queryWrapper);
        addressDefault.setIsDefault(0);
        addressBookService.updateById(addressDefault);
        /*LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId()).
                set(AddressBook::getIsDefault,0);

        addressBookService.update(updateWrapper);*/

        addressBook.setIsDefault(1);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        //条件构造器
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null != BaseContext.getCurrentId(), AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        //SQL:select * from address_book where user_id = ? order by update_time desc
        return R.success(addressBookService.list(queryWrapper));
    }

    @GetMapping("{id}")
    public R<AddressBook> get(@PathVariable Long id){

        log.info("id:{}",id);

        AddressBook addressBook = addressBookService.getById(id);

        return R.success(addressBook);
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }
}
